<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/3/14
 * Time: 10:03
 */
namespace Admin\Controller;
class AdminController extends AdminBase{

    public function index(){
        if(IS_POST){
            $filed="nickname,tel,email,remark";
            $where['id']=self::$userinfo['id'];
            $this->ThisReturn(D('Admin')->db_add($filed,'','save',$where));
        }
        $this->assign(self::$userinfo);
        $this->display();
    }

    public function repeat_user(){
        if(IS_AJAX){
            $this->ajaxReturn(D('Admin','Logic')->repeat_user());
        }

    }
    public function repeat_password(){

        if(IS_AJAX){
            $this->ajaxReturn(D('Admin','Logic')->repeat_password());
        }
    }

    public function psd(){
        if(IS_POST){

        if(D('Admin','Logic')->saveuser()==true){
            session(null);
            $this->success('修改成功，请重新登陆');exit;
        }else{
            $this->error('修改失败，请重试！');
        }

        }
        $this->assign(self::$userinfo);
        $this->display();
    }

    /*后台菜单*/
    public function menu(){

        $this->assign('menu',D('Menu','Logic')->getMenu_str());
        $this->display();
    }
    /*后台菜单添加*/
    public function menu_add(){
        $pid=I('get.pid',0,'int');
        if(IS_POST){
            S('Admin_menu'.session('ldh_userid'),null);//清除缓存
             $this->ThisReturn(D('Menu')->db_add());
         }
        $this->assign('pid',$pid);
        $this->assign('menu_select',D('Menu','Logic')->getMenu_str(1));;
        $this->display();
    }
    /*后台菜单修改*/
    public function menu_save(){
        $id=I('get.id',0,'int');
        if(IS_POST){
            S('Admin_menu'.session('ldh_userid'),null);//清除缓存
            $where['id']=$id;
            $this->ThisReturn(D('Menu')->db_add('','','save',$where));
        }

        $info=M('Menu')->where(array('id'=>$id))->find();
        $this->assign($info);
        $this->assign('menu_select',D('Menu','Logic')->getMenu_str(1));;
        $this->display();

    }
    /*后台菜单删除*/
    public function menu_del(){
        if(IS_AJAX){

            $this->ajaxReturn(D('menu')->menu_del(I('post.id',0,'int')));
        }

    }
    /*后台菜单排序*/
    public function menu_order(){ 
        if(IS_AJAX){
            $listorders=I('post.listorders');
            foreach($listorders as $k=>$v){
                M('menu')->where(array('id'=>$k))->save(array('listorder'=>$v));
            }
            S('Admin_menu'.session('ldh_userid'),null);//清除缓存

            $res['status']='y';$this->ajaxReturn($res);

        }


    }
    /*角色权限*/
    public function role(){

        $this->assign('roles',M('role')->field('menu_ids',true)->select());
        $this->display();
    }
    /*编辑角色*/
    public function role_edit(){
        $id=I('get.id','','int');
        if($_POST){
            $data=I('','','strip_tags');
            $data['menu_ids']=implode(',',$data['menu_ids']);
            if($id){
                $re=DB_edit('role','',$data,'save',array('id'=>$id));
            }else{
                $re=DB_edit('role','',$data);
            }
            $this->ThisReturn($re);
         }
        $menu=D('Menu','Logic')->getMenu();
        $this->assign('menu',$menu['menu_tree']);
        if($id){
            $role=M('role')->where(array('id'=>$id))->find();
            $this->assign($role);
        }
        $this->display();
    }
    /*删除菜单*/
    public function role_del(){

        $id=I('get.id','','int');
        if($id){
            $where['id']=$id;
            $this->ajaxReturn(DB_del('role',$where));
        }

    }
    /*管理员*/
    public function admin(){
        $role_id=I('get.role_id','','int');
        $fields="usermd5,password";
        $where['role_id']=array('neq',0);
        if($role_id){
            $where['role_id']=$role_id;
        }

        $where['id']=array('neq',self::$userinfo['id']);
        $res=M('admin')->field($fields,true)->where($where)->select();
        foreach($res as $k=>$v){
            if($v['role_id']==0){
                $res[$k]['role_name']='超级管理员';
            }else{
                $res[$k]['role_name']=M('role')->where(array('id'=>$v['role_id']))->getField('role_name');
                $res[$k]['role_name']=$res[$k]['role_name']?$res[$k]['role_name']:'无任何权限';
            }

        }
        $this->assign('lists',$res);
        $this->display();
    }
    public function admin_edit(){

        $id=I('get.id','','int');
        if($id==self::$userinfo['id']){$this->error('不能编辑自己!');}
        if($_POST){
            $data=I('','','strip_tags');

            if($data['newpassword1'] || !$id){
                if(strlen($data['newpassword2'])<3){$this->error('密码太短!');}
                if($data['newpassword2']!=$data['newpassword1']){
                    $this->error('两次密码不一样！');
                }
                $data['password']=D('Admin')->Md5_password($data['newpassword1']);
                $data['usermd5']=D('Admin')->Md5_password($data['username']);
            }

            if($id){
                $re=DB_edit('admin','',$data,'save',array('id'=>$id));
            }else{
                $re=DB_edit('admin','',$data);
            }
            $this->ThisReturn($re);
        }

        if($id){
            $fields="usermd5,password";
            $where['id']=$id;
            $where['role_id']=array('neq',0);
            $info=M('admin')->field($fields,true)->where($where)->find();
            if(!$info){$this->error('操作错误!');}
            $this->assign($info);
        }


        $this->assign('roles',M('role')->where(array('status'=>1))->field('id,role_name')->select());
        $this->display();
    }
    /*删除管理员*/
    public function admin_del(){
        $id=I('get.id','','int');
        if($id){
            $where['id']=$id;
            $this->ajaxReturn(DB_del('Admin',$where));
        }


    }
}